import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

import '../controller/userController.dart';
import '../widget/GoodsGridView.dart';
import '../widget/PageViewSlid.dart';

class IndexPage extends StatefulWidget {
  const IndexPage({super.key});

  @override
  State<IndexPage> createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {
  UserController userController = Get.find<UserController>();
  List<String> imageList = [
    "http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-15/6d202d8e-bb47-4f92-9523-f32ab65754f4.jpg",
    "http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-15/dfc11bb0-4af5-4e9b-9458-99f615cc685a.jpg",
    "http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-15/1ba86bcc-ae71-42a3-bc3e-37b662f7f07e.jpg",
    "http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-15/e83efb1b-309c-46f7-98a3-f1fefa694338.jpg",
    "http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-15/4a79180a-1a5a-4042-8a77-4db0b9c800a8.jpg",
  ];


  @override
  void initState() {
    super.initState();
    userController.updateUser();

  }

  //itemGridview组件
  List<Widget> _getCategoryGrid() {
    List<Widget> temList = [];
    for (int i = 0; i < 10; i++) {
      temList.add(Container(
          alignment: Alignment.center,
          child: Column(
            children: [
              Expanded(
                child: Image.network(
                  "http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-05-06/201516e3-25d0-48f5-bcee-7f0cafb14176.png?quality=95&imageView",
                  width: 60,
                ),
              ),
              const SizedBox(
                height: 2.5,
              ),
              const Text(
                "居家",
                style: TextStyle(fontSize: 10),
              )
            ],
          )));
    }
    return temList;
  }

  late RxList<dynamic> itemList = [].obs;

  //推荐gridview组件
  List<Widget> _getRecommandGrid() {
    List<Widget> temList = [];
    for (int i = 0; i < 4; i++) {
      temList.add(Container(
          padding: EdgeInsets.symmetric(horizontal: 7),
          decoration: BoxDecoration(
            //背景颜色
            color: Colors.white,
            border: Border.all(width: 1, color: Colors.black12),
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const Text(
                "特惠推荐",
                style: TextStyle(fontSize: 13),
              ),
              SizedBox(
                height: 5,
              ),
              Row(
                children: [
                  Expanded(
                      child: Image.network(
                          "https://yanxuan-item.nosdn.127.net/0a2fff6e0b55178fbbe99d97d94d20a8.png",
                          width: 20)),
                  SizedBox(
                    width: 3.5,
                  ),
                  Expanded(
                      child: Image.network(
                    "https://yanxuan-item.nosdn.127.net/eaec90be370a438e565e10189b7a390a.png",
                    width: 20,
                  )),
                ],
              ),
            ],
          )));
    }
    return temList;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: PreferredSize(
          preferredSize: const Size.fromHeight(100),
          child: AppBar(
            flexibleSpace: SafeArea(
              child: Container(
                decoration: const BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage('assets/images/navigator_bg.png'),
                    fit: BoxFit.cover,
                  ),
                ),
                child: Column(
                  children: [
                    const Padding(
                      padding: EdgeInsets.only(top: 15, left: 30, right: 30),
                      child: Row(
                        children: [
                          // Image.asset('images/logo.png', width: 166, height: 39),
                          Expanded(
                            child: Padding(
                              padding: EdgeInsets.only(left: 20),
                              child: Text(
                                '新鲜 · 亲民 · 快捷',
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 15,
                                  // border: Border(left: BorderSide(color: Colors.white, width: 1)),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.symmetric(
                          horizontal: 30, vertical: 16),
                      child: Container(
                        height: 30,
                        decoration: BoxDecoration(
                          color: Colors.white.withOpacity(0.5),
                          borderRadius: BorderRadius.circular(32),
                        ),
                        child: const Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Padding(
                              padding: EdgeInsets.only(left: 26),
                              child: Text(
                                '搜索商品',
                                style: TextStyle(
                                    fontSize: 14, color: Colors.white),
                              ),
                            ),
                            Padding(
                              padding: EdgeInsets.all(10),
                              child: Icon(Icons.search_rounded,
                                  size: 15, color: Colors.white),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
            backgroundColor: Colors.transparent,
            elevation: 0,
          ),
        ),
        body: Container(
          //通过rgba修改颜色
          color: const Color.fromRGBO(235, 235, 235, 1),
          child: ListView(children: [
            //轮播图
            PageViewSlid(
              duration: 3500,
              imageList: imageList,
              isUseIndexDots: true,
              height: 100,
            ),
            //分类标签
            GridView.count(
              shrinkWrap: true,
              physics: const NeverScrollableScrollPhysics(),
              padding: const EdgeInsets.all(10),
              crossAxisCount: 5,
              crossAxisSpacing: 10,
              mainAxisSpacing: 20,
              children: _getCategoryGrid(),
            ),
            //推荐区
            GridView.count(
              shrinkWrap: true,
              physics: const NeverScrollableScrollPhysics(),
              padding: const EdgeInsets.all(10),
              crossAxisCount: 2,
              childAspectRatio: 1.5,
              children: _getRecommandGrid(),
            ),
            //商品展示区
            const GoodsGridView()
          ]),
        ));
  }
}
